package cn.yusei.teamTribe.controller.file;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.yusei.teamTribe.constant.Modules;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.exception.FileNotExistsException;
import cn.yusei.teamTribe.exception.ModuleNotExistsException;
import cn.yusei.teamTribe.exception.ReadFileException;
import cn.yusei.teamTribe.exception.ReadInputStreamException;
import cn.yusei.teamTribe.exception.WriteByteException;
import cn.yusei.teamTribe.service.file.FileService;
import cn.yusei.teamTribe.util.ResponseUtil;
import cn.yusei.teamTribe.util.StringUtil;

@Controller
public class FileController {
	
	@Autowired
	private FileService fileService;
	@Value("${fileNameLength}")
	private int fileNameLength = 32;
	@Value("${maxPortraitSize}")
	private int maxPortraitSize;  //默认3M
	@Value("${maxWallpaperSize}")
	private int maxWallpaperSize;
	@Value("${maxPhotoSize}")
	private int maxPhotoSize;  //默认 10M
	@Value("${maxPostImageSize}")
	private int maxPostImageSize;  //默认 10M
	@Value("${maxVideoSize}")
	private int maxVideoSize;  //默认 500M
	
	private boolean validSize(long size, int moduleId)
	{
		switch(moduleId)
		{
			case Modules.PORTRAIT:
				return size <= maxPortraitSize;
			case Modules.POST_IMAGE:
				return size <= maxPostImageSize;
			case Modules.TEAM_PHOTO:
				return size <= maxPhotoSize;
			case Modules.TEAM_VIDEO:
				return size <= maxVideoSize;
			case Modules.TEAM_WALLPAPER:
				return size <= maxWallpaperSize;
		}
		return false;
	}
	
	@RequestMapping(value="/file/upload")
	public void upload(@RequestAttribute int id,
			@RequestParam("file") MultipartFile image, @RequestParam int moduleId, PrintWriter out) {
		if(!Modules.isModule(moduleId)) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		String fileName = null;
		try{
			long size = image.getSize();
			if(!validSize(size, moduleId)){
				ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
				return;
			}
			fileName = fileService.putFile(moduleId, id, image.getInputStream(), true);
		}
		catch (ReadInputStreamException | WriteByteException | IOException e){
			e.printStackTrace();
			System.out.println("error!");
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("fileName", fileName);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value="/file/download")
	public void download(@RequestParam String fileName, @RequestParam int moduleId, HttpServletResponse out) {
		if(StringUtil.haveNullOrEmpty(fileName) || fileName.length() != fileNameLength)
			return;
		byte[] bytes = null;
		try{
			bytes = fileService.getFile(moduleId, 0, fileName);
		}
		catch (FileNotExistsException e){
			e.printStackTrace();
			return;
		}
		catch (ModuleNotExistsException | ReadFileException e){
			e.printStackTrace();
			return;
		}
		ResponseUtil.responseByteArray(out, bytes);
	}
}